/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.controller;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author Administrador
 */
import com.exception.DataException;
import com.javabeans.*;
import com.util.*;
import com.model.*;
import com.db.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.Calendar;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class OperacionesDoc extends HttpServlet {

    HttpServletRequest request;
    HttpServletResponse response;
    HttpSession session;
    String msg = "";
    
    private String PAGE = "";
    private static final String ERROR_PAGE = "WEB-INF/error.jsp";
    private static final String ERROR_AJAX = "WEB-INF/errorAjax.jsp";
    boolean isForward = true; // inicializamos con forward....
    Usuario s_Docente = null;
    
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        this.request = request;
        this.response = response;
        session = request.getSession();
        int accion = Integer.parseInt(request.getParameter("accion"));        
        if(session.getAttribute("s_docente") != null){
            s_Docente = (Usuario)session.getAttribute("s_docente");
            try{
                switch(accion){
                    case 1 : indexSubirNota(); break;                           // accion = 1 = inicio para ver horarios
                    case 2 : indexAdminNota(); break;                           // accion = 2 = inicio para ver horarios
                    default : System.out.println(" accion = "+accion+" no existe en Docentes.java"); throw new DataException("accion = "+accion+" no existe en Docentes.java");
                }
            }catch(DataException e){
                throw new ServletException(e);
            }
        }else
            PAGE = "web/index.jsp?msg=Indentifiquese. La Session no existe!";
        if(PAGE.length() > 0){
            if(isForward){
                RequestDispatcher dispatch = request.getRequestDispatcher(PAGE);
                dispatch.forward(request,response);
            }else{
                response.sendRedirect(PAGE);
            }
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>

    private void indexSubirNota() throws DataException{
        PAGE = "WEB-INF/DOCENTE/operaciones/subirNota/indexSubirNota.jsp";
    }

    private void indexAdminNota() throws DataException{
        Connection c = null;
        try{
            c = Conector.getConexion();            
            //version 2
            GrupoManager gManager = ManagerFactory.getGrupoManagerInstance(c);
            List<Grupo> grupos =gManager.getGrupoXmateriaYdocente(s_Docente.getIdUsuario());
            request.setAttribute("grupos", grupos);
            
            PAGE = "WEB-INF/DOCENTE/operaciones/adminNotas/adminNota.jsp";
        }catch(DataException e){
            System.out.println(UtilDate.getDateTime()+" Error! Inscripciones -> ofertaMaterias() e: "+e.getMessage());
            request.setAttribute("msgError", UtilDate.getDateTime()+" Inscripciones -> ofertaMaterias() DataException "+e.getMessage());
            PAGE = ERROR_PAGE;
        }catch(Exception ex){
            System.out.println(UtilDate.getDateTime()+" Error! Inscripciones -> ofertaMaterias() ex: "+ex.getMessage());
            request.setAttribute("msgError", UtilDate.getDateTime()+" Inscripciones -> ofertaMaterias() Exception ex: "+ex.getMessage());
            PAGE = ERROR_PAGE;
        }finally{
            SqlManager.closeConexion(c);
        }
    }
}
